<?php
/**
*
* 功能描述（用户数据）
*
* @author suoya <jlusuoya@gmail.com>
* @version 1
*/

class ModelAdmin extends ModelBase
{
	function AddAdmin($data)
	{
		$id = $this->db->Insert("admin", $data);
		return $id;
	}

	function Login($username, $pwd)
	{
		$where = "password = '" . md5($pwd) . "' and (mobile = '$username' or email = '$username' or name = '$username')";
		$data  = $this->db->getRow("admin", 'id, mobile, name, truename, p_cake, p_order, p_prod, p_dely, p_msg, p_user, p_log, p_adm', $where);
		if($data)
			$this->db->Execute("update admin set lastlogin = CURRENT_TIMESTAMP where id = " . $data['id']);
		return $data;
	}

	function GetBaseInfo($userid)
	{
		$fild = "`mobile`, `name`, `email`, `truename`, `desp`";
		$data = $this->db->GetRow("admin", $fild, "id = $userid");
		return $data;
	}

	function GetInfo($userid)
	{
		$fild = "`id`, `mobile`, `name`, `email`, `truename`, `regtime`, `lastlogin`, `desp`, p_cake, p_order, p_prod, p_dely, p_msg, p_user, p_log, p_adm";
		$data = $this->db->GetRow("admin", $fild, "id = $userid");
		return $data;
	}

	function GetAdminList($page)
	{
		$data  = $this->db->getRow("admin", 'count(1) cnt', "1");
		if($data['cnt'] == 0)
			return array('cnt' => 0, 'pageCnt' => 0, 'data' => array());

		$retData = array('cnt' => $data['cnt'], 'pageCnt' => ceil($data['cnt'] / ADM_PAGESIZE));
		$limit = ADM_PAGESIZE * ($page - 1) . ", " . ADM_PAGESIZE;
		$fild = "`id`, `mobile`, `name`, `email`, `truename`, `regtime`, `lastlogin`, `desp`, p_cake, p_order, p_prod, p_dely, p_msg, p_user, p_log, p_adm";
		$data = $this->db->getAll("admin", $fild, "1", '', $limit);
		$retData['data'] = $data;
		return $retData;
	}

	function SetPwd($id, $pwd)
	{
		$where = "`id` = $id";
		$data = array(
			'password' => md5($pwd),
		);
		$cnt = $this->db->Update("admin", $data, $where);
		return $cnt;
	}

	function SetPerm($id, $data)
	{
		$where = "`id` = $id";
		$cnt = $this->db->Update("admin", $data, $where);
		return $cnt;
	}
}

